#include <REGX52.H>
#include "Timer0_1ms.h"
#include "Key.h"
#include "Delay.h"
#include "digital.h"
#include "AT24C02.h"

unsigned char KeyNum , Loc , R_S,Time[3] = {0};
	
void main()
{
	
	Timer0_Init();
	
	while(1)
	{		
		Set_Num(1,Time[0]%10);
		Set_Num(2,Time[0]/10);
		Set_Num(3,11);
		Set_Num(4,Time[1]%10);
		Set_Num(5,Time[1]/10);
		Set_Num(6,11);
		Set_Num(7,Time[2]%10);
		Set_Num(8,Time[2]/10);
		
		
		if(Time[0] >= 100) { Time[0] = 0; Time[1]++;} 
		if(Time[1] >= 60)  { Time[1] = 0; Time[2]++;} 
		if(Time[2] >= 60)  { Time[2] = 0;} 
		
		
		if(KeyNum == 1)//启动和停止
		{
			R_S = !R_S;
		}
		
		if(KeyNum == 2)//置0
		{
			Time[0] = 0;
			Time[1] = 0;
			Time[2] = 0;
			R_S = 1;
		}
		if(KeyNum == 3)//将时间写进芯片
		{
			AT24C02_Write(0,Time[0]);
			Delay(5);
			AT24C02_Write(1,Time[1]);
			Delay(5);
			AT24C02_Write(2,Time[2]);
			Delay(5);
		}
		if(KeyNum == 4)//读出芯片中的时间
		{
			Time[0] = AT24C02_Read(0);
			Time[1] = AT24C02_Read(1);
			Time[2] = AT24C02_Read(2);
		}
	}
}

void Timer0_Rountine(void) interrupt 1
{
	static unsigned int Count1, Count2,Count3; //计次
	
	TL0 = 0x66;				
	TH0 = 0xFC;	
	
	
	Count1++;
	
	if(Count1 >= 10)
	{
		
		Key_Loop();
		Count1 = 0;
	
	}
	
	Count2++;
	
	if(Count2 >= 2)
	{
		
		Nixie_Loop();
		Count2 = 0;
		
	}
	
	Count3++;
	
	if(Count3 >= 10 && R_S == 0)
	{
		Time[0]++;
		
		Count3 = 0;
	
	}
	

	KeyNum = Re_Value();
		
		
}